package com.quark.framework.utility;

import java.util.Date;
import java.util.List;

public class QuarkUtilArrayConverter {

	public static Date[] makeDateArray(String arrString)
	{
		String[] array = arrString.split(",");
		Date[] retArray = new Date[array.length];
		for(int i = 0 ; i < array.length; i ++)
		{
			try
			{
				retArray[i] = QuarkUtilDateHelper.parseDate(array[i]);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return null;
			}
		}
		return retArray;
	}
	
	public static String makeArrayString(Date[] arrDate, String marker)
	{
		String array = "";
		for(int i = 0; i < arrDate.length; i++)
		{
			array += marker + QuarkUtilDateHelper.getDateString(arrDate[i]) + marker;
			if(i < arrDate.length - 1)
				array += ",";
		}
		return array;
	}

	public static String makeArrayString(Double[] arrDouble, String marker)
	{
		String array = "";
		for(int i = 0; i < arrDouble.length; i++)
		{
			array += marker + arrDouble[i].toString() + marker;
			if(i < arrDouble.length - 1)
				array += ",";
		}
		return array;
	}

	public static String makeArrayString(Float[] arrFloat, String marker)
	{
		String array = "";
		for(int i = 0; i < arrFloat.length; i++)
		{
			array += marker + arrFloat[i].toString() + marker;
			if(i <  arrFloat.length - 1)
				array += ",";
		}
		return array;
	}

	public static String makeArrayString(Integer[] arrInt, String marker)
	{
		String array = "";
		for(int i = 0; i < arrInt.length; i++)
		{
			array += marker + arrInt[i].toString() + marker;
			if(i <  arrInt.length - 1)
				array += ",";
		}
		return array;
	}

	public static String makeArrayString(Object[] array, String marker)
	{
		String arrayString = "";
		for(int i = 0; i < array.length; i++)
		{
			arrayString += marker + array[i].toString() + marker;
			if(i <  array.length - 1)
				arrayString += ",";
		}
		return arrayString;
	}
	
	public static String makeArrayString(List<Object> list, String marker)
	{
		String array = "";
		for(int i = 0; i < list.size(); i++)
		{
			array += marker + list.get(i).toString() + marker;
			if(i <  list.size() - 1)
				array += ",";
		}
		return array;		
	}
	
	public static Double[] makeDoubleArray(String arrString)
	{
		String[] array = arrString.split(",");
		Double[] retArray = new Double[array.length];
		for(int i = 0 ; i < array.length; i ++)
		{
			try
			{
				retArray[i] = Double.valueOf(array[i]);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return null;
			}
		}
		return retArray;
	}

	public static Integer[] makeIntegerArray(String arrString)
	{
		String[] array = arrString.split(",");
		Integer[] retArray = new Integer[array.length];
		for(int i = 0 ; i < array.length; i ++)
		{
			try
			{
				retArray[i] = Integer.valueOf(array[i]);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return null;
			}
		}
		return retArray;
	}
	public static String[] toStringArray(Object obj)
	{
		if(!checkArrayType(String.class, obj))
			return null;
		Object[] objArray = (Object[])obj;
		String[] retArray = new String[objArray.length];
		for(int i = 0; i < retArray.length; i ++)
		{
			retArray[i] = (String)objArray[i];
		}
		return retArray;
	}
	public static Integer[] toIntegerArray(Object obj)
	{
		if(!checkArrayType(Integer.class, obj))
			return null;
		Object[] objArray = (Object[])obj;
		Integer[] retArray = new Integer[objArray.length];
		for(int i = 0; i < retArray.length; i ++)
		{
			retArray[i] = (Integer)objArray[i];
		}
		return retArray;
	}
	public static Double[] toDoubleArray(Object obj)
	{
		if(!checkArrayType(Double.class, obj))
			return null;
		Object[] objArray = (Object[])obj;
		Double[] retArray = new Double[objArray.length];
		for(int i = 0; i < retArray.length; i ++)
		{
			retArray[i] = (Double)objArray[i];
		}
		return retArray;
	}
	public static Date[] toDateArray(Object obj)
	{
		if(!checkArrayType(Double.class, obj))
			return null;
		Object[] objArray = (Object[])obj;
		Date[] retArray = new Date[objArray.length];
		for(int i = 0; i < retArray.length; i ++)
		{
			retArray[i] = (Date)objArray[i];
		}
		return retArray;
	}	
	public static boolean checkArrayType(Class<?> arrClass, Object arr)
	{
		if(!arr.getClass().isArray())
			return false;
		if(!arrClass.equals(arr.getClass().getComponentType()))
			return false;
		return true;
	}
}
